package com.tsingteng.cosfun.base;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.mvp.presenter.BasePresenter;
import com.tsingteng.cosfun.mvp.view.IView;
import com.tsingteng.cosfun.widget.MyRecycleView;


/**
 * 描述:请求网络的fragment封装
 * 创建人: 肖利
 * 时间: 2018/7/30
 * qq: (872179966)
 */

public abstract class BasePresenterFragment<P extends BasePresenter, V extends IView> extends BaseFragment implements IView, BaseQuickAdapter.RequestLoadMoreListener,
        MyRecycleView.MyRecyclerViewListener, BaseQuickAdapter.OnItemClickListener {
    protected P mPresenter;
    protected BaseQuickAdapter mAdapter;
    private MyRecycleView mRv;

    @Override
    protected void initView() {
        mPresenter = createPresenter();
        initAdapter();
        //绑定view
        attachView();
        //逻辑
        initData(rootView);
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {
        mAdapter = createAdapter();
        if (mAdapter != null) {
            if (initHeader() != null) mAdapter.addHeaderView(initHeader());
            mAdapter.setOnLoadMoreListener(this);
            mAdapter.setOnItemClickListener(this);
        }
        mRv = createRecycleView();
        if (mRv != null) {
            mRv.setLayoutManager(new LinearLayoutManager(getContext()));
            mRv.setAdapter(mAdapter);
            mRv.setMyRefreshListener(this);
        }
    }

    @Override
    public void showLoading(String msg) {
        showLoadingDialog(msg);
    }

    @Override
    public void hideLoading() {
        hideLoadingDialog();
    }

    @Override
    public void showFail(String msg) {
        if (mRv != null && mAdapter != null && mAdapter.getData().size() > 0) {
            mAdapter.loadMoreFail();
            mRv.refreshComplete();
        }
        showToast(msg);
    }

    @Override
    public void showError() {
        if (mRv != null && mAdapter != null && mAdapter.getData().size() > 0) {
            mAdapter.loadMoreFail();
            mRv.refreshComplete();
        }

    }

    @Override
    public void showEmpty() {
        if (mRv != null || mAdapter != null) {
            mAdapter.loadMoreFail();
            mRv.refreshComplete();
        }
    }


    /**
     * 绑定view
     */
    private void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }

    /**
     * 得到上个页面传递的数据
     *
     * @param bundle
     */
    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter.removeAllDisposable();
        }
    }

    /**
     * 数据逻辑
     *
     * @param rootView
     */
    protected abstract void initData(View rootView);

    /**
     * 创建Presenter对象
     *
     * @return
     */
    protected abstract P createPresenter();

    /**
     * 如果有header的话初始化header
     *
     * @return
     */
    protected abstract View initHeader();

    /**
     * 有recycleview的话初始化
     *
     * @return
     */
    protected abstract MyRecycleView createRecycleView();

    /**
     * 创建adapter
     *
     * @return
     */
    protected abstract BaseQuickAdapter createAdapter();


    @Override
    public void onRefresh() {
        mAdapter.getData().clear();
        refresh();

    }


    /**
     * 刷新数据
     */
    protected abstract void refresh();

}
